﻿/// <reference path="jquery-1.8.2.min.js" />
var m_blog_getuserrelation = "http://uc.tool.hexun.com/getrelation";
var m_blog_addattention = "http://uc.tool.hexun.com/addattention";
var m_blog_cancelattention = "http://uc.tool.hexun.com/cancelattention";
var m_blog_getarticlemore = "/ArticleOperate.aspx";

function CancelRelation(userid) {
    jQuery.ajax({
        type: "get",
        url: m_blog_cancelattention,
        dataType: "jsonp",
        data: { attentionuserid: userid, t: Math.random() },
        success: function (data, textStatus) {
            if (data.status == 1) {
                jQuery("#relationforme").text("+加关注");
                jQuery("#relationforme").removeClass("bngzn").addClass("bngz");
                jQuery("#relationforme").unbind("click");
                jQuery("#relationforme").click(function () {
                    AddAttention(userid);
                });
            }
        }
    });
}
function AddAttention(userid) {
    jQuery.ajax({
        type: "get",
        url: m_blog_addattention,
        dataType: "jsonp",
        data: { attentionuserid: userid, sourceid: 3, t: Math.random() },
        success: function (data, textStatus) {
            if (data.status == 1) {
                jQuery("#relationforme").text("取消关注");
                jQuery("#relationforme").removeClass("bngz").addClass("bngzn");
                jQuery("#relationforme").unbind("click");
                jQuery("#relationforme").click(function () {
                    CancelRelation(userid);
                });
            }
        }
    });
}


function GetUserRelation(userid) {
    jQuery.ajax({
        type: "get",
        url: m_blog_getuserrelation,
        dataType: "jsonp",
        data: { userId: userid, t: Math.random() },
        success: function (data, textStatus) {
            if (data.status == 1) {
                var result = data.result;
                if (result.UserRelation == 1 || result.UserRelation == 3) {
                    jQuery("#relationforme").text("取消关注");
                    jQuery("#relationforme").removeClass("bngz").addClass("bngzn");
                    jQuery("#relationforme").unbind("click");
                    jQuery("#relationforme").click(function () {
                        CancelRelation(userid);
                    });
                } else {
                    jQuery("#relationforme").text("+加关注");
                    jQuery("#relationforme").removeClass("bngzn").addClass("bngz");
                    jQuery("#relationforme").unbind("click");
                    jQuery("#relationforme").click(function () {
                        AddAttention(userid);
                    });
                }
            }
        }
    });
}

function GetArticleCount() {
    jQuery.ajax({
        type: "get",
        url: "http://click.tool.hexun.com/mbloglink.aspx",
        dataType: "jsonp",
        data: { blogid: jQuery("#blogid").val(), articleidlist: jQuery("#articleidlist").val(), t: Math.random() },
        success: function (data, textStatus) {
            if (data.status == 1) {
                var result = data.countlist;
                for (var i = 0; i < result.length; i++) {
                    jQuery("#clickcount" + result[i].articleid).text("(" + result[i].clickcount + ")");
                    jQuery("#commentcount" + result[i].articleid).text("(" + result[i].commentcount + ")");
                    jQuery("#pointcount" + result[i].articleid).text("(" + result[i].votecount + ")");
                }

            }
        }
    });
}

jQuery("#click_more").click(function () {
    var pagenum = jQuery("#pagenum").val();
    pagenum = parseInt(pagenum) + 1;
    jQuery("#pagenum").val(pagenum);
    jQuery.ajax({
        type: "get",
        url: m_blog_getarticlemore,
        dataType: "jsonp",
        data: { uid: watchuserid, pagenum: pagenum, t: Math.random() },
        success: function (data, textStatus) {
            var html = [];
            if (data.articlelist != undefined && data.articlelist != null && data.articlelist.length > 0) {
                var idlist = [];
                for (var i = 0; i < data.articlelist.length; i++) {
                    idlist.push(data.articlelist[i].articleId);
                    html.push("<li>");
                    html.push("<h1>");
                    html.push("<a href=\"/detail.aspx?aid=" + data.articlelist[i].articleId + "\">");
                    html.push("" + data.articlelist[i].title + "");
                    html.push("</a></h1>");
                    html.push("<div class=\"text clearfix\">");

                    //html.push("<img src=\"http://minimg.hexun.com/i9.hexunimg.cn/2014-09-04/168191108_c166x118.jpg\" original=\"http://minimg.hexun.com/i9.hexunimg.cn/2014-09-04/168191108_c166x118.jpg\" alt=\"图片\">");
                    if (data.articlelist[i].sourceUrl.indexOf("<img") == 0 && data.articlelist[i].sourceUrl.indexOf("tag=\"photowap\"/>") == data.articlelist[i].sourceUrl.length - 16) {
                        html.push(data.articlelist[i].sourceUrl);
                    }


                    html.push("<p>");
                    html.push("<a href=\"/detail.aspx?aid=" + data.articlelist[i].articleId + "\">");
                    html.push("" + data.articlelist[i].brief + "");
                    html.push("</a>");
                    html.push("</p>");
                    html.push("<time>" + data.articlelist[i].postTime.substring(0, 16) + "</time>");
                    html.push("<div class=\"bn\">");

                    html.push("<span id=\"pointcount" + data.articlelist[i].articleId + "\">(0)</span><img src=\"images/icon3.png\" />");
                    html.push("<span id=\"commentcount" + data.articlelist[i].articleId + "\">(0)</span><img src=\"images/icon2.png\" />");
                    html.push("<span id=\"clickcount" + data.articlelist[i].articleId + "\" >(0)</span><img src=\"images/icon1.png\" />");

                    html.push("</div>");
                    html.push("</div>");
                    html.push("</li>");
                }
                jQuery("#articleidlist").val(idlist.join(','));
            }
            jQuery("#news_list").append(html.join(""));
            GetArticleCount();
            if (data.ismore != undefined && data.ismore != null) {
                if (data.ismore == true) {
                    jQuery("#click_more").show();
                } else {
                    jQuery("#click_more").hide();
                }
            }
        }
    });
});

jQuery("#m_blog_zan").click(function () {
    jQuery.ajax({
        type: "get",
        url: "/VoteArticle.aspx",
        dataType: "jsonp",
        data: { aid: m_blog_articleid, uid: m_blog_uid, t: Math.random() },
        success: function (data, textStatus) {
            if (data != null && data != undefined) {
                if (data.state == 0) {
                    alert(data.msg);
                }
                if (data.state == 1) {
                    alert(data.msg);
                }
            }
        }
    });
});

jQuery("#plcontent").focus(function () {
    jQuery(this).removeClass("bgico");
    var text = jQuery.trim(jQuery("#plcontent").html()).replace(/&nbsp;/g, '');
    jQuery("#plcontent").text(text);
    wid($('.plipt'));
    $('.plipt .pltext').width(iw);
    if ($(".plyzm span img").length == 0) {
        $(".plyzm span").html('<img src="/vcode.aspx?aid=' + m_blog_articleid + '&v=' + Math.random() + '" id="vcode" alt="点我刷新" />');
    }
});
jQuery("#plcontent").blur(function () {
    var text = jQuery.trim(jQuery("#plcontent").html()).replace(/&nbsp;/g, '');
    if (text == "") {
        jQuery(this).addClass("bgico");
    }
    wid($('.plipt'));
    $('.plipt .pltext').width(iw);
});

jQuery("#pl_ipt").click(function () {
    var comment = jQuery.trim(jQuery("#plcontent").html()).replace(/&nbsp;/g, '');
    var vcode = jQuery("#plyzmz").val();
    jQuery.ajax({
        type: "get",
        url: "/postcomment.aspx",
        dataType: "jsonp",
        data: { aid: m_blog_articleid, uid: m_blog_uid, comment: encodeURIComponent(comment), vcode: vcode, t: Math.random() },
        success: function (data, textStatus) {
            if (data != null && data != undefined) {
                if (parseInt(data.status) == 0) {
                    alert(data.msg);
                    $(".plyzm span").html('<img src="/vcode.aspx?aid=' + m_blog_articleid + '&v=' + Math.random() + '" id="vcode" alt="点我刷新" />');
                }
                if (data.status == 1) {
                    var html = [];
                    html.push("<dl>");
                    html.push("<dt>");
                    if (data.msg.logo != "") {
                        html.push("<img alt=\"\" src=\"" + data.msg.logo + "\" />");
                    } else {
                        html.push("<img alt=\"\" src=\"http://logo.tool.hexun.com/0-40.jpg\" />");
                    }
                    html.push("</dt>");
                    html.push("<dd>");
                    html.push("<div class=\"pl_user1\">");
                    html.push(data.msg.nickname);
                    html.push("</div>");
                    html.push("<div class=\"pl_content1 ft28\" >");
                    html.push(data.msg.Content);
                    html.push("</div>");
                    html.push("</dd>");
                    html.push("<dd class=\"pl_pubtime1\">");
                    html.push(data.msg.date);
                    html.push("</dd>");
                    html.push("</dl>");

                    jQuery("#newestcomment").prepend(html.join(""));
                    wid($('.plipt'));
                    $('.plipt .pltext').width(iw);
                    setTimeout(function () {
                        $('.pl_content1').each(function () {
                            if ($(this).height() > 100) {
                                $(this).css('max-height', '5.6em');
                                if ($(this).closest("dd").find(".alltxt").length == 0) {
                                    $(this).after('<p class="alltxt">(<span>展开全部内容</span><b></b>)</p>');
                                }
                            }
                        });
                    }, 200);

                    $('.alltxt').live("click", function () {
                        $(this).prev('.pl_content1').css('max-height', 'none').end().remove();
                    });

                    //点击查看更多
                    $('.morepl').live("click", function () {
                        var pnum = parseInt($('#pn').html()) + 1;
                        $('#pn').html(pnum);
                        loadComment(m_blog_blogname, m_blog_articleid, m_blog_blogid, pnum);
                    });

                    jQuery("#plcontent").html("");
                    jQuery("#plyzmz").val("");
                    jQuery("#vcode").click();
                    jQuery("#plcontent").blur();
                }
            }
        }
    });
});

$('#vcode').click(function () {
    var url = "http://m.blog.hexun.com/vcode.aspx?aid=" + m_blog_articleid + "&v=" + new Date().getTime();

    $(this).attr('src', url);
});
var iw;
function wid(obj) {
    if ($(window).width() >= 640) {
        iw = obj.width() - parseInt(obj.find('.pltext').css('padding-left')) - 4;
    } else {
        iw = obj.width() - parseInt(obj.find('.pltext').css('padding-left')) - 2;
    }
}
function loadComment(blogname, aid, blogid, page) {
    var qsData = { "blogname": blogname, "articleid": aid, "blogid": blogid, "page": page };
    jQuery.ajax({
        type: "get",
        url: "http://m.blog.hexun.com/GetComment.aspx",
        dataType: "jsonp",
        data: qsData,
        success: function (data, textStatus) {
            if (data != null && data != undefined) {
                var html = [];
                for (var i = 0; i < data.comment.length; i++) {
                    html.push("<dl>");
                    html.push("<dt>");
                    if (data.comment[i].Logo != "") {
                        html.push("<img alt=\"\" src=\"" + data.comment[i].Logo + "\" />");
                    } else {
                        html.push("<img alt=\"\" src=\"http://logo.tool.hexun.com/0-40.jpg\" />");
                    }
                    html.push("</dt>");
                    html.push("<dd>");
                    html.push("<div class=\"pl_user1\">");
                    html.push(data.comment[i].nickname);
                    html.push("</div>");
                    html.push("<div class=\"pl_content1 ft28\" >");
                    html.push(data.comment[i].Content);
                    html.push("</div>");
                    html.push("</dd>");
                    html.push("<dd class=\"pl_pubtime1\">");
                    html.push(data.comment[i].PostTime);
                    html.push("</dd>");
                    html.push("</dl>");
                }
                if (data.ismore == "0") {
                    $(".morepl").hide();
                }
                if (data.ismore == "1") {
                    $(".morepl").show();
                }
                jQuery("#newestcomment").append(html.join(""));
                wid($('.plipt'));
                $('.plipt .pltext').width(iw);
                setTimeout(function () {
                    $('.pl_content1').each(function () {
                        if ($(this).height() > 100) {
                            $(this).css('max-height', '5.6em');
                            if ($(this).closest("dd").find(".alltxt").length == 0) {
                                $(this).after('<p class="alltxt">(<span>展开全部内容</span><b></b>)</p>');
                            }
                        }
                    });
                }, 200);
                $('.alltxt').live("click", function () {
                    $(this).prev('.pl_content1').css('max-height', 'none').end().remove();
                });

            }
        }
    });
}

$('.morepl').click(function () {
    var pnum = parseInt($('#pn').html()) + 1;
    $('#pn').html(pnum);
    loadComment(m_blog_blogname, m_blog_articleid, m_blog_blogid, pnum);
});